package test.yzx.pproject.act1;


import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import test.yzx.pproject.R;
import test.yzx.pproject.util.BaseItemAnimator;
import test.yzx.pproject.util.BaseRecyclerAdapter;
import yzx.common.view.util.RecyclerViewUtil;

public class RecyclerViewActivity extends AppCompatActivity{

    private RecyclerView rv;
    private int dy = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_view);
        for (int i = 0; i < 40; i++) {
            Person p = new Person();
            p.name="IAM"+i;
            p.age = i+"";
            data.add(p);
        }


        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setHasFixedSize(true);
        rv.addItemDecoration(new Divide());
        rv.setItemAnimator(new ItemAnimator());

        rv.setLayoutManager(new LinearLayoutManager(this, 1, false));
        rv.setAdapter(new RVAdapter());

        RecyclerViewUtil.bindScrollRange(rv, new RecyclerViewUtil.OnRangeChangedListener() {
            public void onScrollRangeChange(int totalDx, int totalDy) {

            }
        });
    }

    private List<Person> data = new ArrayList<>(40);


    private class RVAdapter extends BaseRecyclerAdapter{
        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View layout = getLayoutInflater().inflate(R.layout.item_rv_1, parent, false);
            return new BaseViewHolder(layout);
        }
        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            TextView nameTv = (TextView) holder.getView(R.id.tv_name);
            holder.getView(R.id.btn_delete).setTag(holder);
            holder.getView(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    BaseViewHolder holder = (BaseViewHolder) v.getTag();
                    data.remove(holder.getAdapterPosition());
                    notifyItemRemoved(holder.getAdapterPosition());
                }
            });
            nameTv.setText(data.get(position).name);
        }
        @Override
        public int getItemCount() {
            return data.size();
        }
    }


    private class Person{
        String name;
        String age;
    }



    /* 分割线 */
    private class Divide extends RecyclerView.ItemDecoration{
        private final int itemOffsets = 50;
        private Paint paint = new Paint();
        {
            paint.setColor(Color.parseColor("#e2e2e2"));
        }
        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            /// 在item的下边画一个圆 ///
            Paint p = new Paint();p.setColor(Color.RED);
            c.drawCircle(parent.getWidth()/2, 50, 50, p);

            for (int i = 0; i < parent.getChildCount(); i++) {
                View child = parent.getChildAt(i);
                c.drawRect(0, child.getBottom(), parent.getWidth(), child.getBottom()+itemOffsets, this.paint);
            }
        }
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            /// 在item的上边画一个圆 ///
            Paint p = new Paint();p.setColor(Color.BLUE);
            c.drawCircle(parent.getWidth()/2, 150, 50, p);
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            //上下缩进50 左右缩进20
            outRect.set(20, 50, 20, itemOffsets);
        }
    }


    /* Item 动画 */
    private class ItemAnimator extends BaseItemAnimator{
        @Override
        protected Runnable setRemoveAnimateProperties(final RecyclerView.ViewHolder holder, ViewPropertyAnimatorCompat anim) {
            anim.translationX(-holder.itemView.getWidth());
            return new Runnable() {
                @Override
                public void run() {
                    ViewCompat.setTranslationX(holder.itemView, 0);
                }
            };
        }
        @Override
        public long getRemoveDuration() {
            return 300;
        }
    }

}
